package grape.house.rest.estatelicence.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.base.rest.client.dict.DictClient;
import grape.base.service.dict.po.Dict;
import grape.common.exception.runtime.RBaseException;
import grape.house.rest.building.mapper.BuildingWebMapper;
import grape.house.rest.building.vo.BuildingPresalePermitVo;
import grape.house.rest.building.vo.BuildingVo;
import grape.house.rest.estatelicence.form.EstateLicenceListForm;
import grape.house.service.building.api.IBuildingService;
import grape.house.service.building.po.Building;
import grape.house.service.estatelicence.query.EstateLicenceListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.house.rest.estatelicence.form.EstateLicenceCreateForm;
import grape.house.rest.estatelicence.form.EstateLicenceUpdateForm;
import grape.house.rest.estatelicence.form.EstateLicenceListPageForm;
import grape.house.rest.estatelicence.vo.EstateLicenceVo;
import grape.house.rest.estatelicence.mapper.EstateLicenceWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.house.service.estatelicence.po.EstateLicence;
import grape.house.service.estatelicence.api.IEstateLicenceService;
import java.util.List;
/**
 * <p>
 * 楼盘许可证 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/house/estatelicence")
@Api(tags = "楼盘许可证相关接口")
public class EstateLicenceController extends BaseController<EstateLicenceVo, EstateLicence> {

    @Autowired
    private EstateLicenceWebMapper currentWebMapper;
    @Autowired
    private IEstateLicenceService currentService;

    @Autowired
    private DictClient dictClient;
    @Autowired
    private TransBuildingPresalePermitVoHelper transBuildingPresalePermitVoHelper;



     @ApiOperation("添加楼盘许可证")
     @PreAuthorize("hasAuthority('estatelicence:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public EstateLicenceVo create(@RequestBody @Valid EstateLicenceCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }

         EstateLicence po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询楼盘许可证")
     @PreAuthorize("hasAuthority('estatelicence:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public EstateLicenceVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除楼盘许可证")
     @PreAuthorize("hasAuthority('estatelicence:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新楼盘许可证")
     @PreAuthorize("hasAuthority('estatelicence:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public EstateLicenceVo update(@PathVariable String id,@RequestBody @Valid EstateLicenceUpdateForm uf) {
         EstateLicence po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }


    @ApiOperation("不分页查询楼盘许可证")
    @PreAuthorize("hasAuthority('estatelicence:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<EstateLicenceVo> list(EstateLicenceListForm listForm) {
        EstateLicence po = currentWebMapper.formToPo(listForm);
        queryPermitTypeDictCodeSupport(po, listForm.getPermitTypeDictCode());
        EstateLicenceListQuery estateLicenceListQuery = new EstateLicenceListQuery();
        estateLicenceListQuery.setPoQuery(po);
        return super.list(estateLicenceListQuery);
    }

    @ApiOperation("分页查询楼盘许可证")
    @PreAuthorize("hasAuthority('estatelicence:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<EstateLicenceVo> listPage(EstateLicenceListPageForm listPageForm) {
         EstateLicence po = currentWebMapper.formToPo(listPageForm);
        queryPermitTypeDictCodeSupport(po, listPageForm.getPermitTypeDictCode());
        EstateLicenceListQuery estateLicenceListQuery = new EstateLicenceListQuery();
        estateLicenceListQuery.setPoQuery(po);
         return super.listPage(estateLicenceListQuery,listPageForm);
     }

    /**
     * 类型支持字典编码查询
     * @param po
     * @param permitTypeDictCode
     */
    private void queryPermitTypeDictCodeSupport(EstateLicence po, String permitTypeDictCode) {
        if (!isStrEmpty(permitTypeDictCode)) {
            Dict dictByCode = dictClient.getDictByCode(permitTypeDictCode);
            if (dictByCode != null) {
                po.setPermitTypeDictId(dictByCode.getId());
            }
        }
    }

    @Override
    public EstateLicenceVo transVo(EstateLicenceVo estateLicenceVo) {
        estateLicenceVo = transBuildingPresalePermitVoHelper.transVo(estateLicenceVo);
        return super.transVo(estateLicenceVo);
    }
}
